import os
from pathlib import Path
from zipfile import ZipFile, ZIP_DEFLATED


def dir2pak(root_path, zip_path):
    root_path = Path(root_path)
    # 将整个目录压缩成zip文件
    with ZipFile(zip_path, 'w', ZIP_DEFLATED) as zf:
        # 循环写入文件和文件夹
        for root, dirs, files in os.walk(root_path):
            for _ in files:
                __ = os.path.join(root, _)
                # 判断文件是否在根目录且是.pak文件或.zip文件
                if os.path.isfile(__) and (os.path.basename(root_path) in __) and (
                        os.path.splitext(__)[1] in ['.pak', '.zip']):
                    continue
                # 获取当前文件的相对路径
                arc_name = os.path.relpath(__, root_path)
                zf.write(__, arc_name)
    return zip_path
